{include file="common/mainHead"/}
<div class="admin-main fadeInUp animated">
    <blockquote class="layui-elem-quote">
       数据库中共有{$tableNum}张表，共计{$total}
        <a href="javascript:void(0)" onclick="gobackup(this)" class="layui-btn layui-btn-small pull-right">备份</a>
    </blockquote>
    <fieldset class="layui-elem-field">
        <legend>数据列表</legend>
        <div class="layui-field-box table-responsive">
            <table class="layui-table table-hover">
                <thead>
                <tr>
                    <th><input type="checkbox" id="selected-all" onclick="$('input[name*=\'backs\']').prop('checked', this.checked);"></th>
                    <th>数据库表</th>
                    <th>记录条数</th>
                    <th>占用空间</th>
                    <th>类型</th>
                    <th>编码</th>
                    <th>创建时间</th>
                    <th>说明</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {volist name="dataList" id="v"}
                <tr>
                    <td><input type="checkbox" name="backs[]" value="{$v.Name}"></td>
                    <td>{$v.Name}</td>
                    <td>{$v.Rows}</td>
                    <td>{$v.Data_length|format_bytes}</td>
                    <td>{$v.Engine}</td>
                    <td>{$v.Collation}</td>
                    <td>{$v.Create_time}</td>
                    <td>{$v.Comment}</td>
                    <td>
                        <a href="javascript:;" onclick="return optimize('{$v['Name']}');" class="layui-btn layui-btn-normal layui-btn-mini">优化</a>
                        <a href="javascript:;" onclick="return repair('{$v['Name']}');" class="layui-btn layui-btn-mini">修复</a>
                    </td>
                </tr>
                {/volist}
                </tbody>
            </table>

        </div>
    </fieldset>
</div>
{include file="common/mainFoot"/}
<script>
    layui.config({
        base: '__ADMIN__/plugins/layui/modules/'
    });
    layui.use(['icheck','layer'], function() {
        var $ = layui.jquery, layer = parent.layer === undefined ? layui.layer : parent.layer;

        $('#selected-all').on('ifChanged', function(event) {
            var $input = $('.layui-table tbody tr td').find('input');
            $input.iCheck(event.currentTarget.checked ? 'check' : 'uncheck');
        });
    });
    function gobackup(obj){
        var a = [];
        $('input[name*=backs]').each(function(i,o){
            if($(o).is(':checked')){
                a.push($(o).val());
            }
        });

        $(obj).addClass('disabled');
        $(obj).html('备份进行中...');
        $.post("{:url('database/backup')}",{tables:a},function(data){
            data = eval('('+data+')');
            if(data.code==1){
                $(obj).removeClass('disabled');
                $(obj).html('备份');
                layer.msg(data.msg,{time:500});
            }else{
                layer.msg(data.msg,{time:1800});
            }
        });
    }
    function optimize(name) {
        $.post("{:url('database/optimize')}",{tableName:name},function(res){
            if(res.code > 0){
                layer.msg(res.msg,{time:1800},function(){
                    window.location.href = res.url;
                });
            }else{
                layer.msg(res.msg,{time:1800});
            }
        });
    }
    function repair(name) {
        $.post("{:url('database/repair')}",{tableName:name},function(data){
            if(data.code>0){
                layer.msg(data.msg,{time:1800}, function(){
                    window.location.href = data.url;
                });
            }else{
                layer.msg(data.msg,{time:1800});
            }
        });
    }
</script>